package modelcontroller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Mariam
 */
public class Conversation {

    private User remoteUser;
    private String history;

    /**
     * Constructs a conversation between the local user and another user.
     * <p>
     * Initially, the conversation history is empty
     *
     * @param remoteUser the remote user
     * @see User
     */
    public Conversation(User remoteUser) {
        this.remoteUser = remoteUser;
        this.history = "";
    }

    /**
     * Gets the remote user linked to the conversation
     *
     * @return remote user associated to the conversation
     */
    public User getRemoteUser() {
        return remoteUser;
    }

    /**
     * Gets the history of the conversation
     *
     * @return history of the conversation
     */
    public String getHistory() {
        return history;
    }

    /**
     * Adds (concatenates) a message to the conversation
     *
     * @param msg message to add to the history
     */
    public void addMessage(String msg) {
        DateFormat dateFormat = new SimpleDateFormat("hh:mm");
        Date date = new Date();
        this.history = this.getHistory() + "\n [" + dateFormat.format(date) + "] " + msg;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else {
            Conversation c = (Conversation) obj;
            return this.remoteUser.getUsername().equals(c.getRemoteUser().getUsername());
        }
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + (this.remoteUser != null ? this.remoteUser.hashCode() : 0);
        return hash;
    }
}
